﻿using HangYi_Coffee;
using HangYi_Coffee_Class;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

using System.Text.Json.Serialization;
using System.Text.Json;

namespace HangYi_Coffee.Controllers
{
    [Route("api/[controller]/[action]")]
        [ApiController]
    public class DetailsController : Controller
    {
        #region 获取商品表
        [HttpGet]
        public async Task<IActionResult> DetailList()
        {

            try
            {
                using HangYi_Coffee_Class._DbContext myDbContext = new HangYi_Coffee_Class._DbContext();

                var sortsWithDetails = await myDbContext.Sorts
                   .Include(x => x.Commoditys)
                   .ThenInclude(d => d.SugarOptions)
                   .Include(x => x.Commoditys)
                   .ThenInclude(d => d.TemperatureOptions)
                   .Include(x => x.Commoditys)
                   .ThenInclude(d => d.CupTypeOptions)
                   .Include(x => x.Commoditys)
                   .ThenInclude(d => d.IceOptions)
                   .ToListAsync();
                var options = new JsonSerializerOptions
                {
                    ReferenceHandler = ReferenceHandler.Preserve,
                    
                };

                 return Ok(JsonSerializer.Serialize(sortsWithDetails, options));
               //return Ok(sortsWithDetails);
            }
            catch (Exception ex)
            {
                // Log the error, e.g. with ILogger<YourController>    
                return StatusCode(500, ex.Message);
            }
        }
        #endregion
        #region 获取特定商品
        [HttpGet]
        public IActionResult GetCommodity(int commodityid)
        {
            using var myDbContext = new _DbContext();
            var commodity = myDbContext.Commoditys
                .Include(c => c.SugarOptions)
                .Include(s => s.TemperatureOptions)
                .Include(s => s.CupTypeOptions)
                .Include(s => s.IceOptions)
                .SingleOrDefaultAsync(c => c.Id == commodityid)
                .Result; // 等待异步操作完成并获取结果

            if (commodity == null)
            {
                return NotFound();
            }

            return Ok(commodity); // 直接返回商品数据的集合
        }
        #endregion
    }
}

//增加商品
//Detail detail = new Detail();
//detail.Id = 1;
//detail.Name = "Test";

//Cup_typeOption cup_Type = new Cup_typeOption();
//cup_Type.Id = 1;
//cup_Type.Cup_ption = "大杯";
//Cup_typeOption cup_Type1 = new Cup_typeOption();
//cup_Type1.Id = 2;
//cup_Type1.Cup_ption = "小杯";
//detail.CupTypeOptions = new List<Cup_typeOption>();
//detail.CupTypeOptions.Add(cup_Type1);
//detail.CupTypeOptions.Add(cup_Type);

//IceOption ice = new IceOption();
//ice.Id = 1;
//ice.Ice_option = "少冰";
//IceOption ice1 = new IceOption();
//ice1.Id = 2;
//ice1.Ice_option = "多冰";
//detail.IceOptions = new List<IceOption>();
//detail.IceOptions.Add(ice);
//detail.IceOptions.Add(ice1);

//SugarOption sugar = new SugarOption();
//sugar.Id = 1;
//sugar.Sugar_Option = "五分糖";
//SugarOption sugar1 = new SugarOption();
//sugar1.Id = 2;
//sugar1.Sugar_Option = "七分糖";
//detail.SugarOptions = new List<SugarOption>();
//detail.SugarOptions.Add(sugar);
//detail.SugarOptions.Add(sugar1);

//TemperatureOption Temperature = new TemperatureOption();
//Temperature.Id = 1;
//Temperature.Temperature_ption = "热";
//TemperatureOption Temperature1 = new TemperatureOption();
//Temperature1.Id = 2;

//Temperature1.Temperature_ption = "冷";
//detail.TemperatureOptions = new List<TemperatureOption>();
//detail.TemperatureOptions.Add(Temperature);
//detail.TemperatureOptions.Add(Temperature1);

//myDbContext.Details.Add(detail);
//myDbContext.SaveChanges();
